package cn.bingoogolapple.bgabanner.demo.ui;

import cn.bingoogolapple.bgabanner.BGABanner;
import cn.bingoogolapple.bgabanner.demo.App;
import cn.bingoogolapple.bgabanner.demo.ResourceTable;
import cn.bingoogolapple.bgabanner.demo.model.RefreshModel;
import cn.bingoogolapple.bgabanner.demo.provider.GridViewWithHeaderAndFooter;
import cn.bingoogolapple.bgabanner.demo.provider.ListViewProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.window.dialog.ToastDialog;

import java.util.ArrayList;
import java.util.List;

public class ListViewDemoSlice extends AbilitySlice implements BGABanner.Delegate<Image, Object>, BGABanner.Adapter<Image, Object> {
    private GridViewWithHeaderAndFooter listview;
    private BGABanner banner_main_default;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_list_view_demo);
        initView();
        initData();
        setListener();
    }

    private void setListener() {
        BGABanner.Delegate mDelegate = ListViewDemoSlice.this;
        if (banner_main_default != null) {
            banner_main_default.setDelegate(mDelegate);
            banner_main_default.setDelegate(mDelegate);
        }
    }

    private void initView() {
        listview = (GridViewWithHeaderAndFooter) findComponentById(ResourceTable.Id_list_view);
        listview.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                new ToastDialog(getContext()).setText("" + i).show();
            }
        });
        LayoutScatter layoutScatter = LayoutScatter.getInstance(this);
        Component header = layoutScatter.parse(ResourceTable.Layout_layout_header, null, false);
        banner_main_default = (BGABanner) header.findComponentById(ResourceTable.Id_banner_main_default);
        banner_main_default.setmPlaceholderDrawableResId(ResourceTable.Media_holder);
        banner_main_default.setIconNoElement(ResourceTable.Media_hui_yuan_no);
        banner_main_default.setIconYesElement(ResourceTable.Media_yuan_yes);
        listview.addHeaderView(header);
    }

    private void initData() {
        // listView
        initListView();

        // 轮播
        initBanner();
    }

    private void initBanner() {
        loadData(banner_main_default);
    }

    private void initListView() {
        loadContentData();
    }

    private void loadData(final BGABanner banner) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(ResourceTable.Media_bg7_img);
        list.add(ResourceTable.Media_bg8_img);
        list.add(ResourceTable.Media_bg9_img);
        list.add(ResourceTable.Media_bg10_img);
        list.add(ResourceTable.Media_bg_img);
        BGABanner.Adapter mAdapter = (BGABanner.Adapter) ListViewDemoSlice.this;
        banner.setAdapter(mAdapter);
        banner.setData(list, null);
    }

    @Override
    public void fillBannerItem(BGABanner banner, Image itemView, Object model, int position) {
        if (model instanceof String) {
            itemView.setPixelMap(App.getInstance().getBitmap(String.valueOf(model)));
        } else if (model instanceof Integer) {
            itemView.setPixelMap((Integer) model);
        }
    }

    @Override
    public void onBannerItemClick(BGABanner banner, Image itemView, Object model, int position) {
        new ToastDialog(getContext()).setText("点击了第" + (position + 1) + "页").setAlignment(0).show();
    }

    /**
     * 加载内容列表数据
     *
     * @noinspection checkstyle:MagicNumber
     */
    private void loadContentData() {
        List<RefreshModel> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(new RefreshModel("内容", "内容"));
        }
        ListViewProvider listViewProvider = new ListViewProvider(list, ListViewDemoSlice.this, 1);
        listview.setAdapter(listViewProvider);
    }
}
